<?php

/**
 * This page handles the 'delauth' Delegated Authentication action.
 * When you create a Windows Live application, you must specify the URL 
 * of this handler page.
 */

// Load common settings.  For more information, see settings.php.
defined('AUTH_SERVICE') or define('AUTH_SERVICE','live');

require_once '../server.php';

// Specify true to log messages to Web server logs.
$DEBUG = false;

$INDEX = 'connect.php'; 

$OFFERS = 'WL.Basic,WL.Emails';

// $OFFERS = 'WL.Basic,WL.Emails,Contacts.View';
// Comma-delimited list of offers to be used.

// Application key file: store in an area that cannot be
// accessed from the Web.
$KEYFILE = APP_PATH . '/live/config.xml';

// Name of cookie to use to cache the consent token.
$COOKIE = 'delauthtoken-contact';

$COOKIETTL = time() + (10 * 365 * 24 * 60 * 60);

// URL of Delegated Authentication index page.

$HANDLER = 'delauth-connect.php';
// Default handler for Delegated Authentication.

$PROXY_SVR = "";

include 'windowslivelogin.php';

// Initialize the WindowsLiveLogin module.
$wll = WindowsLiveLogin::initFromXml($KEYFILE);

$wll->setDebug($DEBUG);

// Extract the 'action' parameter, if any, from the request.
$action = @$_REQUEST['action'];

if ($action == 'delauth') {
  $consent = $wll->processConsent($_REQUEST);

// If a consent token is found, store it in the cookie that is 
// configured in the settings.php file and then redirect to 
// the main page.
  if ($consent) {
    setcookie($COOKIE, $consent->getToken(), $COOKIETTL);
  }
  else {
    setcookie($COOKIE);
  }
}

header("Location: $INDEX");
?>
